package com.sc.datacollect.netty.server;

import com.google.gson.Gson;
import com.sc.datacollect.auth.AuthFilter;
import com.sc.datacollect.auth.ResultMessage;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SecureServerHandler extends ChannelInboundHandlerAdapter {
	private Gson gson = new Gson();

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) {
		try {
			AuthFilter auth = new AuthFilter(String.valueOf(msg));
			String result = auth.authmessage();
			if (result != null) {
				ResultMessage resultMsg = new ResultMessage(result);
				ctx.writeAndFlush(gson.toJson(resultMsg));
				ctx.close();
			} else {
				ctx.fireChannelRead(msg);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			ctx.writeAndFlush(e.toString());
			ctx.close();
			return;
		}
	}
}
